/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrameAjuste.java
 *
 * Created on Jun 2, 2012, 10:35:41 PM
 */

package ajustes;

import ajustes.db.*;
import javax.swing.DefaultComboBoxModel;

/**
 *
 * @author Jessica Ordoñez
 */
public class FrameAjuste extends javax.swing.JFrame {
    
    private Conexion conn;
    private final Usuario user;
    private ListaEmpresaPermiso lep;
    
    private DefaultComboBoxModel modelCboEmp = new DefaultComboBoxModel();
    private DefaultComboBoxModel modelCboDiv = new DefaultComboBoxModel();
    
    /** Creates new form FrameAjuste
     * @param conn 
     * @param user 
     */
    public FrameAjuste(Conexion conn, Usuario user) {
        initComponents();
        this.conn = conn;
        this.user = user;
        setTitle("Ajuste - Apertura de Ajuste 1.0");
        
        //Estable los Model para cada combo
        cboEmp.setModel(modelCboEmp);
        cboDiv.setModel(modelCboDiv);
        
        //Carga por primera vez los combos
        cargarCombos();
        
    }
    
    /**
     * Carga los combo con las empresar y las divisiones.
     */
    private void cargarCombos(){
        this.lep = new ListaEmpresaPermiso(user);
        cargarEmpresa();
    }
    
    /**
     * Carga el combo empresa y solo se ejecuta una sola vez.
     * Nota: es llamado por cargarCombos
     * @see ajustes.FrameAjuste#cargarCombos() 
     */
    private void cargarEmpresa(){
        //Carga todas las empresa a la que el usuario tiene permiso
        this.lep.cargarEmpresas(this.conn.getCnx());
        
        //Carga las empresa al combo
        modelCboEmp.removeAllElements();
        for( EmpresaPermiso ep: lep.getEmpresas() ){
            modelCboEmp.addElement(ep);
        }
        //Para que se llene el combo de divisiones
        cboEmp.setSelectedIndex(0);
    }
    
    /**
     * Carga el combo division, dependiendo de la empresa seleccionada en 
     * el combo de empresas.
     * Nota: este metodo se llama cuando se selecciona una empresa
     * @see ajustes.FrameAjuste#cboEmpActionPerformed(java.awt.event.ActionEvent) 
     */
    private void cargarDivision(){
        //Obtener la empresa que tiene selecionada
        EmpresaPermiso ep = (EmpresaPermiso) modelCboEmp.getSelectedItem();
        
        modelCboDiv.removeAllElements();
        for( DivisionPermiso dp : ep.getDivisiones() ){
            modelCboDiv.addElement(dp);
        }
        cboDiv.setSelectedIndex(0);
    }
    
    /**
     * Limpia el codigo.
     */
    private void clear() {
        txtCod.setText("");
        cboEmp.requestFocus();
        //cboDiv.setSelectedIndex(0);
    }
    
    /**
     * Realiza la desliberacion del ajuste.
     */
    private void desliberar() {
        
    }
    
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnInfo = new javax.swing.JPanel();
        lbEmp = new javax.swing.JLabel();
        lbDiv = new javax.swing.JLabel();
        lbCod = new javax.swing.JLabel();
        cboEmp = new javax.swing.JComboBox();
        cboDiv = new javax.swing.JComboBox();
        txtCod = new javax.swing.JTextField();
        btnOk = new javax.swing.JButton();
        btnClear = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        pnInfo.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Información", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14))); // NOI18N

        lbEmp.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbEmp.setText("Empresa:");
        lbEmp.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lbDiv.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbDiv.setText("División:");
        lbDiv.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lbCod.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbCod.setText("Código:");
        lbCod.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        cboEmp.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        cboEmp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboEmpActionPerformed(evt);
            }
        });

        cboDiv.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        txtCod.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        txtCod.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtCodKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout pnInfoLayout = new javax.swing.GroupLayout(pnInfo);
        pnInfo.setLayout(pnInfoLayout);
        pnInfoLayout.setHorizontalGroup(
            pnInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnInfoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnInfoLayout.createSequentialGroup()
                        .addComponent(lbEmp, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cboEmp, 0, 269, Short.MAX_VALUE))
                    .addGroup(pnInfoLayout.createSequentialGroup()
                        .addComponent(lbDiv, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cboDiv, 0, 269, Short.MAX_VALUE))
                    .addGroup(pnInfoLayout.createSequentialGroup()
                        .addComponent(lbCod, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtCod, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        pnInfoLayout.setVerticalGroup(
            pnInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnInfoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbEmp, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cboEmp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbDiv, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cboDiv, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbCod, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnOk.setMnemonic('D');
        btnOk.setText("Desliberar");
        btnOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOkActionPerformed(evt);
            }
        });

        btnClear.setMnemonic('L');
        btnClear.setText("Limpiar");
        btnClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClearActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnInfo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnOk)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnInfo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnClear, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnOk, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOkActionPerformed
        desliberar();
    }//GEN-LAST:event_btnOkActionPerformed

    private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClearActionPerformed
        clear();
    }//GEN-LAST:event_btnClearActionPerformed

    private void txtCodKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCodKeyTyped
        //Evita que ingrese letras
        if( !(evt.getKeyChar() >= '0' && evt.getKeyChar() <= '9') ){
            evt.consume();
        }
    }//GEN-LAST:event_txtCodKeyTyped

    private void cboEmpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboEmpActionPerformed
        cargarDivision();
    }//GEN-LAST:event_cboEmpActionPerformed

    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClear;
    private javax.swing.JButton btnOk;
    private javax.swing.JComboBox cboDiv;
    private javax.swing.JComboBox cboEmp;
    private javax.swing.JLabel lbCod;
    private javax.swing.JLabel lbDiv;
    private javax.swing.JLabel lbEmp;
    private javax.swing.JPanel pnInfo;
    private javax.swing.JTextField txtCod;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the user
     */
    public Usuario getUser() {
        return user;
    }

    

    

}
